//
//  VoiceSongCollectionViewCell.m
//  SmileRead
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "VoiceSongCollectionViewCell.h"
#import "VoiceMusicModel.h"
#import "Path.h"
#import "MBProgressHUD.h"

@interface VoiceSongCollectionViewCell ()<UIScrollViewDelegate, UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *webView;

@property (nonatomic, retain) UIScrollView *upView;

@property (nonatomic, retain) UIImageView *upImageView;

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) MBProgressHUD *mb;

@end

@implementation VoiceSongCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 180 * heightFit, self.frame.size.width, self.frame.size.height)];
        self.webView.scrollView.delegate = self;
        self.webView.delegate = self;
        [self.contentView addSubview:self.webView];
        [_webView release];
        
        
        self.upView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 180 * heightFit)];
        [self.contentView addSubview:self.upView];
        self.upView.backgroundColor = [UIColor cyanColor];
        self.upView.delegate = self;
        [_upView release];
        
        self.upImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.upView.frame.size.width, self.upView.frame.size.height)];
        [self.upView addSubview:self.upImageView];
        [_upImageView release];

        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 * heightFit, self.frame.size.width, 60 * heightFit)];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.tintColor = [UIColor blueColor];
        //self.titleLabel.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:self.titleLabel];
        
        self.mb = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
        self.mb.labelText = @"正在加载";
        [self.mb hide:YES afterDelay:6];
        [self.mb show:YES];
        
    }
    return self;
}

- (void)setModel:(VoiceMusicModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        
        NSURL *url = [NSURL URLWithString:[_model.playInfo objectForKey:@"webview_url"]];
        NSURLRequest *requset = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:requset];
        [Path pathWithImageView:self.upImageView URLString:_model.coverimg];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.webView.scrollView) {
        self.upView.frame = CGRectMake(0, 0, self.frame.size.width, (-scrollView.contentOffset.y + 180 * heightFit));
        self.upImageView.frame = CGRectMake(0, 0, self.upView.frame.size.width, self.upView.frame.size.height);
        if (scrollView.contentOffset.y > 60 * heightFit) {
            self.titleLabel.text = self.model.title;
        }
        if (scrollView.contentOffset.y < 80 * heightFit) {
            self.titleLabel.text = @"";
        }
    }
}

// 这样效果好
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.mb removeFromSuperview];
}

- (void)dealloc
{
    [_mb release];
    [_titleLabel release];
    [_upImageView release];
    [_upView release];
    [_webView release];
    [super dealloc];
}


@end
